package com.dmap.officialwebsite.service.impl;

import com.dmap.officialwebsite.constants.OfficialWebsiteConstant;
import com.dmap.officialwebsite.controller.background.param.ArticleParam;
import com.dmap.officialwebsite.controller.background.param.ArticleSelectParam;
import com.dmap.officialwebsite.dao.ArticleMapper;
import com.dmap.officialwebsite.po.Article;
import com.dmap.officialwebsite.po.ArticleExample;
import com.dmap.officialwebsite.service.ArticleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Override
    public Article insert(ArticleParam param) {
        Article article = new Article();
        article.setCompanyId(Long.valueOf(param.getCompanyId()));
        article.setCompanyName(param.getCompanyName());
        article.setArticleTypeId(Long.valueOf(param.getArticleTypeId()));
        article.setArticleTypeName(param.getArticleTypeName());
        article.setTitle(param.getTitle());
        article.setSecondTitle(param.getSecondTitle());
        article.setAddress(param.getAddress());
        article.setAuthor(param.getAuthor());
        article.setCreateTime(System.currentTimeMillis());
        article.setUpdateTime(article.getCreateTime());
        int success = articleMapper.insertSelective(article);
        return (success == 1) ? article : null;
    }

    @Override
    public boolean update(ArticleParam param) {
        Article article = new Article();
        article.setId(Long.valueOf(param.getId()));
        article.setCompanyId(Long.valueOf(param.getCompanyId()));
        article.setCompanyName(param.getCompanyName());
        article.setArticleTypeId(Long.valueOf(param.getArticleTypeId()));
        article.setArticleTypeName(param.getArticleTypeName());
        article.setTitle(param.getTitle());
        article.setSecondTitle(param.getSecondTitle());
        article.setAddress(param.getAddress());
        article.setAuthor(param.getAuthor());
        article.setUpdateTime(System.currentTimeMillis());
        return articleMapper.updateByPrimaryKeySelective(article) == 1;
    }

    @Override
    public boolean deleteById(Long id) {
        return articleMapper.deleteByPrimaryKey(id) == 1;
    }

    @Override
    public List<Article> selectAllByExample(ArticleSelectParam param) {
        ArticleExample example = this.articleSelectExample(param);
        example.setLimitStart((param.getPageIndex() - 1) * param.getPageSize());
        example.setLimitEnd(param.getPageSize());
        return articleMapper.selectByExampleWithBLOBs(example);
    }

    @Override
    public int countRecordNumberByExample(ArticleSelectParam param) {
        return articleMapper.countByExample(this.articleSelectExample(param));
    }

    private ArticleExample articleSelectExample(ArticleSelectParam param) {
        //select * from article
        //where company_id = 2 and article_type_id = 3 and (title like '%a%' or second_title like '%a%' or author like '%a%') limit 1, 10;
        ArticleExample example = new ArticleExample();

        ArticleExample.Criteria criteria01 = example.or();
        ArticleExample.Criteria criteria02 = example.or();
        ArticleExample.Criteria criteria03 = example.or();
        if (param.getCompanyId() != null && StringUtils.isNumeric(param.getCompanyId())) {
            criteria01.andCompanyIdEqualTo(Long.valueOf(param.getCompanyId()));
            criteria02.andCompanyIdEqualTo(Long.valueOf(param.getCompanyId()));
            criteria03.andCompanyIdEqualTo(Long.valueOf(param.getCompanyId()));
        }
        if (param.getArticleTypeId() != null && StringUtils.isNumeric(param.getArticleTypeId())) {
            criteria01.andArticleTypeIdEqualTo(Long.valueOf(param.getArticleTypeId()));
            criteria02.andArticleTypeIdEqualTo(Long.valueOf(param.getArticleTypeId()));
            criteria03.andArticleTypeIdEqualTo(Long.valueOf(param.getArticleTypeId()));
        }
        if (StringUtils.isNotBlank(param.getInputString())) {
            criteria01.andTitleLike("%" + param.getInputString() + "%");
            criteria02.andSecondTitleLike("%" + param.getInputString() + "%");
            criteria03.andAuthorLike("%" + param.getInputString() + "%");
        }
        example.setOrderByClause(OfficialWebsiteConstant.ARTICLE_ORDER_BY);
        return example;
    }

}